#include "DemoController.h"

#include "GameFramework/Character.h"
#include "../Component/DamageComponent.h"

ADemoController::ADemoController()
{
}

void ADemoController::BeginPlay()
{
	Super::BeginPlay();
	GLog -> Log(ELogVerbosity::Type::Warning, "ADemoController::BeginPlay");

	UEnhancedInputLocalPlayerSubsystem* InputSubsystem =
		GetLocalPlayer() -> GetSubsystem<UEnhancedInputLocalPlayerSubsystem>();
	if (InputSubsystem)
	{
		InputSubsystem -> AddMappingContext(InputMappingContext, 0);
	}
}

void ADemoController::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);
}

void ADemoController::SetupInputComponent()
{
	Super::SetupInputComponent();
	UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(InputComponent);
	if (Input && IsValid(Input))
	{
		Input -> BindAction(MoveAction, ETriggerEvent::Triggered, this, &ThisClass::Move);
		Input -> BindAction(LookAction, ETriggerEvent::Triggered, this, &ThisClass::Look);
		Input -> BindAction(FireAction, ETriggerEvent::Triggered, this, &ThisClass::Fire);
	}
}

void ADemoController::Move(const FInputActionValue& Value)
{
	const FVector2D Input = Value . Get<FVector2D>();
	const FRotator Rotation = GetControlRotation();
	const FRotator YawRotation(0, Rotation . Yaw, 0);
	const FVector Forward = FRotationMatrix(YawRotation) . GetUnitAxis(EAxis::X);
	const FVector Right = FRotationMatrix(YawRotation) . GetUnitAxis(EAxis::Y);
	GetCharacter() -> AddMovementInput(Right, Input . Y);
	GetCharacter() -> AddMovementInput(Forward, Input . X);
}

void ADemoController::Look(const FInputActionValue& Value)
{
	const FVector2D Input = Value . Get<FVector2D>();
	AddYawInput(Input . X);
	AddPitchInput(Input . Y);
}

void ADemoController::Fire()
{
	if (GEngine)
	{
		GEngine -> AddOnScreenDebugMessage(-1, 5, FColor::Green, TEXT("开火键"));
	}
	UDamageComponent* DamageComponent = GetCharacter() -> FindComponentByClass<UDamageComponent>();
	if (DamageComponent)
	{
		DamageComponent -> Attack();
	}
}
